!function(){
  var timeRangeSelector = {};
  var fmt = 'MM/DD/YYYY h:mm a';
  var timeRangeCtrl;
  var start = moment().hour(7).minute(0);
  var end = moment();
  
  function echoTimeRange() {
    timeRangeCtrl.val(start.format(fmt) + ' - ' + end.format(fmt));
  }
  
  function isTimeRangeOk(picker){
    return moment(picker.endDate).diff(moment(picker.startDate),'minutes') > 60;
  }
  
  function buildRanges(){
    end = moment();
    return {
      'Today': [start, end],
      'Yesterday': [moment().subtract(1, 'days').hour(7).minute(0), end],
      'Last 3 Days': [moment().subtract(2, 'days').hour(7).minute(0), end],
      'Last 7 Days': [moment().subtract(6, 'days').hour(7).minute(0), end],
      'Last 15 Days': [moment().subtract(14, 'days').hour(7).minute(0), end],
      'Last 30 Days': [moment().subtract(29, 'days').hour(7).minute(0), end],
      'This Month': [moment().startOf('month').hour(7).minute(0), moment().endOf('month')],
      'Last Month': [moment().subtract(1, 'month').startOf('month').hour(7).minute(0), moment().subtract(1, 'month').endOf('month')]
    };
  }
  
  timeRangeSelector.config = function(options){
    console.log('do config here.');
    return this;
  };
  
  timeRangeSelector.getDefaultRange = function(){
    //console.log('getDefaultRange');
    return {from:start.toISOString(),to:end.toISOString()};
  };

  timeRangeSelector.show = function(timeRangeTextID,cb,fail) {
    timeRangeCtrl = $("#"+timeRangeTextID);
    //config the daterangepicker here.
    timeRangeCtrl.daterangepicker({
      startDate: start,
      endDate: end,
      minDate: moment().subtract(180, 'days').toDate(),
      maxDate: moment().toDate(),
      timePicker: true,
      timePickerIncrement: 30,
      ranges: buildRanges(),
      locale: {
        format: fmt
      }
    }, echoTimeRange);

    //daterangepicker event.
    timeRangeCtrl.on('show.daterangepicker', (ev, picker)=>{
      //console.log('show.daterangepicker');
      if(moment().diff(end,'minutes') >=1){
        console.log('need update range.');
        picker.ranges = buildRanges();
        console.log('update range over.');
      }
    });

    timeRangeCtrl.on('apply.daterangepicker', (ev, picker)=>{
      if(isTimeRangeOk(picker)){
        var dataTimeRange = {
          from:picker.startDate.toISOString(),
          to:picker.endDate.toISOString()
        };
        if(cb) cb(dataTimeRange);
      }else{
        console.log('timeRange invalid.');
        if(fail) fail();
        //parent.layer.msg('timeRange invalid.');
      }
    });
  };
  
  this.timeRangeSelector = timeRangeSelector;
}();
